package com.dbq.share;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import static com.dbq.share.YibaShareDelegateActivity.KEY_SHARE_TARGET;
import static com.dbq.share.YibaShareDelegateActivity.KEY_SHARE_WEBPAGE;

/**
 * Created by sunkai on 2017/12/27.
 */

public abstract class BaseAssistActivity extends Activity {

    private boolean mIsFirstIntent;
    protected String uuid;
    protected ShareWebPage shareWebPage;
    private ShareTarget shareTarget;
    protected boolean isResume;

    public boolean isResume() {
        return isResume;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.TOP | Gravity.LEFT);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.x = 0;
        attributes.y = 0;
        attributes.height = 1;
        attributes.width = 1;
        window.setAttributes(attributes);
        mIsFirstIntent = true;
        uuid = getIntent().getExtras().getString(YibaShareDelegateActivity.KEY_UUID, "");
        shareWebPage = getIntent().getExtras().getParcelable(KEY_SHARE_WEBPAGE);
        shareTarget = getIntent().getExtras().getParcelable(KEY_SHARE_TARGET);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isResume = true;
        if (mIsFirstIntent) {
            mIsFirstIntent = false;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final boolean appInstall = appInstall();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (appInstall) {
                                sendMessage();
                            } else {
                                appNotInstall();
                                appNotInstallTips();
                                finish();
                            }
                        }
                    });
                }

            }).start();

        } else {
            ShareCallback callback;
            if ((callback = YibaShare.getShareClient().shareCallback.get(uuid)) != null) {
                callback.shareFail(shareTarget.media, ShareCallback.Code.USER_PRESS_HOME, "user press home");
                finish();
            }

        }

    }

    private void appNotInstall() {
        ShareCallback callback;
        if ((callback = YibaShare.getShareClient().shareCallback.get(uuid)) != null) {
            callback.shareFail(shareTarget.media, ShareCallback.Code.APP_NOT_INSTALL, "app not install");
            YibaShare.getShareClient().shareCallback.remove(uuid);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        isResume = false;
    }

    protected abstract void appNotInstallTips();

    protected abstract boolean appInstall();

    protected abstract void sendMessage();

    protected void startShare() {
        ShareCallback callback;
        if ((callback = YibaShare.getShareClient().shareCallback.get(uuid)) != null) {
            callback.startShare(shareTarget.media);
            callback.showProgress(this);
        }
    }

    protected void shareSuccess() {
        ShareCallback callback;
        if ((callback = YibaShare.getShareClient().shareCallback.get(uuid)) != null) {
            callback.hideProgress();
            callback.shareComplete(shareTarget.media);
            YibaShare.getShareClient().shareCallback.remove(uuid);
        }
    }

    protected void shareCancel() {
        ShareCallback callback;
        if ((callback = YibaShare.getShareClient().shareCallback.get(uuid)) != null) {
            callback.hideProgress();
            callback.shareFail(shareTarget.media, ShareCallback.Code.USER_CANCEL, "user_cancel");
            YibaShare.getShareClient().shareCallback.remove(uuid);
        }
    }

    protected void shareError(String errMsg) {
        ShareCallback callback;
        if ((callback = YibaShare.getShareClient().shareCallback.get(uuid)) != null) {
            callback.hideProgress();
            callback.shareFail(shareTarget.media, ShareCallback.Code.SHARE_ERROR, errMsg);
            YibaShare.getShareClient().shareCallback.remove(uuid);
        }
    }

    private void dismissDialog() {
        ShareCallback callback;
        if ((callback = YibaShare.getShareClient().shareCallback.get(uuid)) != null) {
            callback.hideProgress();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissDialog();
    }
}
